package com.aidex.web.controller.biz;

import com.aidex.biz.domain.OrderDetails;
import com.aidex.biz.service.OrderDetailsService;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.controller.BaseController;
import com.aidex.common.core.domain.R;
import com.aidex.common.core.page.PageDomain;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.SecurityUtils;
import com.aidex.common.utils.poi.ExcelUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 订单详情Controller
 *
 * @author sean
 * @email 2074844820@qq.com
 * @date 2022-10-12
 */
@RestController
@RequestMapping("/biz/bizOrderDetails")
public class OrderDetailsController extends BaseController {
    @Autowired
    private OrderDetailsService orderDetailsService;

    /**
     * 查询订单详情列表
     */
//    @PreAuthorize("@ss.hasPermi('biz:bizOrderDetails:list')")
    @GetMapping("/list")
    public R<PageInfo> list(OrderDetails orderDetails, HttpServletRequest request, HttpServletResponse response) {
        if (!SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            orderDetails.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        orderDetails.setPage(new PageDomain(request, response));
        return R.data(orderDetailsService.findPage(orderDetails));
    }

    /**
     * 获取订单详情详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizOrderDetails:query')")
    @GetMapping(value = "/{id}")
    public R<OrderDetails> detail(@PathVariable("id") String id) {
        return R.data(orderDetailsService.get(id));
    }

    /**
     * 新增订单详情
     */
    @PreAuthorize("@ss.hasPermi('biz:bizOrderDetails:add')")
    @Log(title = "订单详情", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated OrderDetails orderDetails) {
        return R.status(orderDetailsService.save(orderDetails));
    }

    /**
     * 修改订单详情
     */
    @PreAuthorize("@ss.hasPermi('biz:bizOrderDetails:edit')")
    @Log(title = "订单详情", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated OrderDetails orderDetails) {
        return R.status(orderDetailsService.save(orderDetails));
    }


    /**
     * 删除订单详情
     */
    @PreAuthorize("@ss.hasPermi('biz:bizOrderDetails:remove')")
    @Log(title = "订单详情", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.status(orderDetailsService.deleteOrderDetailsByIds(ids));
    }

    /**
     * 获取最大编号
     */
    @PreAuthorize("@ss.hasPermi('biz:bizOrderDetails:query')")
    @GetMapping("/findMaxSort")
    public R findMaxSort() {
        return R.data(orderDetailsService.findMaxSort(new OrderDetails()));
    }

    /**
     * 导出订单详情列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizOrderDetails:export')")
    @Log(title = "订单详情", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(OrderDetails orderDetails) {

        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            orderDetails.setCompanyId(null);
        } else {
            orderDetails.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        List<OrderDetails> list = orderDetailsService.findList(orderDetails);
        ExcelUtil<OrderDetails> util = new ExcelUtil<OrderDetails>(OrderDetails.class);
        return util.exportExcel(list, "订单详情数据");
    }

}
